//
//  WTPlayPersonInfoView.m
//  WTPlayDemo
//
//  Created by ben on 16/7/14.
//  Copyright © 2016年 ben. All rights reserved.
//

#import "WTPlayPersonInfoView.h"
#import "Masonry.h"
#import "UIColor+WTExtend.h"
#import "NSString+WTShowNumber.h"
#import "UIImageView+WebCache.h"
#import "UIImage+WTExtend.h"

@interface WTPlayPersonInfoView ()
@property (nonatomic, strong) UIImageView*  levelImageView;
@property (nonatomic, strong) UIButton*     personInfoButton;
@property (nonatomic, strong) UIImageView*  iconImageView;
@property (nonatomic, strong) UILabel*      nickLabel;
@property (nonatomic, strong) UILabel*      countLabel;
@end


@implementation WTPlayPersonInfoView

- (instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        
        self.layer.cornerRadius = 17;
        self.layer.masksToBounds = YES;
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.2];
        
        [self setup];
    }
    return self;
}


#pragma mark -  setup
- (void)setup{
    
    
    self.iconImageView = ({
        UIImageView* imageView = [[UIImageView alloc] init];
        imageView.layer.cornerRadius = 17;
        imageView.layer.masksToBounds = YES;
        imageView.image = [UIImage wt_imageWithName:@"wt_avatar"];
        [self addSubview:imageView];
        imageView;
    });
    
    self.personInfoButton = ({
        UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        button;
    });
    
    
    self.nickLabel = ({
        UILabel* label = [[UILabel alloc] init];
        label.textColor = [UIColor whiteColor];
        label.font = [UIFont systemFontOfSize:13];
        label.text = @"我叫王尼玛";
        [self addSubview:label];
        label;
    });


    self.countLabel = ({
        UILabel* label = [[UILabel alloc] init];
        label.textColor = [UIColor whiteColor];
        label.font = [UIFont systemFontOfSize:10];
        label.text = [NSString personCount:@"1568985"];
        [self addSubview:label];
        label;
    });

    self.attentionButton = ({
        UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitle:@"关注" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor themeColor] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:12];
        button.layer.cornerRadius = 5;
        button.layer.masksToBounds = YES;
        button.layer.borderColor = [UIColor themeColor].CGColor;
        button.layer.borderWidth = 1;
        [self addSubview:button];
        button;
    });
    
    
    self.isAttentioned = YES;
    [self layout];

}

#pragma mark - layout
- (void)layout{
    
    
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(5);
        make.width.and.height.mas_equalTo(35);
        make.centerY.mas_equalTo(self.mas_centerY);
    }];
    
    [self.personInfoButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.iconImageView);
    }];
    
    [self.nickLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.iconImageView.mas_trailing).offset(4);
        make.top.mas_equalTo(5);
        make.trailing.mas_equalTo(self.attentionButton.mas_leading).offset(-4);
    }];
    
    [self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.nickLabel.mas_leading);
        make.top.mas_equalTo(self.nickLabel.mas_bottom).offset(1);
        make.trailing.mas_equalTo(self.nickLabel.mas_trailing);
    }];
    
    [self.attentionButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(45);
        make.height.mas_equalTo(20);
        make.centerY.mas_equalTo(self.mas_centerY);
    }];
}

#pragma mark - 
- (void)buttonPressed:(id)sender{
    
    NSInteger index = 0;
    if(sender == self.personInfoButton){
        index = 0 ;
    }else if(sender == self.attentionButton){
        index = 1;
    }else {
        
    }
    
    if(self.pressedBlock){
        self.pressedBlock(index);
    }
}


#pragma mark - setter
- (void)setIsAttentioned:(BOOL)isAttentioned{

    if(isAttentioned == self.isAttentioned){
        return;
    }
    _isAttentioned = isAttentioned;
    if(isAttentioned){
        [self.attentionButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(0);
        }];
    }else {
        [self.attentionButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(45);
        }];
    }
}

- (void)setAvatar:(NSString *)avatar{
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:avatar] placeholderImage:[UIImage wt_imageWithName:@"wt_avatar"]];
}

- (void)setNick:(NSString *)nick{
    [self.nickLabel setText:nick];
}

- (void)setPersonCount:(NSString *)personCount{
    self.countLabel.text = personCount;
}

@end
